package com.eliota.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eliota.entity.Menu;
import com.eliota.mapper.MenuMapper;
import com.eliota.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MG
 * @since 2023-03-28
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    public MenuMapper menuMapper;

    @Override
    public boolean deleteById(Integer id) {

        //假删除
        int i = menuMapper.updateStatue(id);
        if(i != 0){
            return true;
        }else
            return false;

    }

    public boolean deleteBathByIds(List<Integer> ids){
        for (Integer id : ids) {
            int i = menuMapper.updateStatue(id);
            if(i == 0){
                return false;
            }
        }
        return true;
    }

    @Override
    public List<Menu> findMenus(String menuName) {
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(menuName)){
            menuQueryWrapper.like("menu_name",menuName);
        }
        menuQueryWrapper.eq("statue",1);

        List<Menu> list = list(menuQueryWrapper);
        //找出父节点为空的一级菜单
        List<Menu> parentNodes = list.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
        for (Menu menu : parentNodes) {
            //找出pid等于父级菜单的id
            menu.setChildren(list.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        }

        return parentNodes;
    }

}
